package com.example.douyin.demo;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.douyin.R;

public class DemoActivity extends AppCompatActivity {

    private ImageView iv;
    private MyView mv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo);
        initView();
        mv.start(5);
    }

    private void initView() {
        iv = (ImageView) findViewById(R.id.iv);

        mv = (MyView) findViewById(R.id.mv);
    }

    public void change1(View view) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anmi1);
        iv.startAnimation(animation);
    }

    public void change2(View view) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anmi2);
        iv.startAnimation(animation);
    }

    public void change3(View view) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.anmi3);
        iv.startAnimation(animation);
    }

    public void change4(View view) {
       /* Animation animation = AnimationUtils.loadAnimation(this, R.anim.anmi4);
        animation.setInterpolator(new CycleInterpolator(4));
        iv.startAnimation(animation);*/
        //让小爱心变透明
        /*ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "alpha", 1, 0, 0.5f);
        animator.setDuration(3000);
        animator.start();*/
        //小爱心组合效果
        ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "alpha", 1, 0, 0.5f);
        ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0, 60);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(iv, "scaleX", 1, 2, 1);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 1, 2, 1);
        rotation.setInterpolator(new CycleInterpolator(2));
        AnimatorSet set = new AnimatorSet();
        set.play(animator).with(rotation).before(scaleX).before(scaleY);
        set.setDuration(3000);
        set.start();
    }

    public void change5(View view) {
        ValueAnimator animator = ValueAnimator.ofInt(100, 800, 200, 1000);
        animator.setDuration(3000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                int width = (int) animation.getAnimatedValue();//获取当前的动画值
                ViewGroup.LayoutParams params = view.getLayoutParams();//获取button参数
                params.width = width;
                view.setLayoutParams(params);
            }
        });
        animator.start();
    }
}